﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daycare.Models;
using Daycare.Services;


namespace Daycare
{
    public partial class Staff : System.Web.UI.Page
    {
        /* Variables */
        private UserService userService;
        private ImageService imageService;
        private MembershipService membershipService;
        private DocumentService documentService;

        /* Event Handling */
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                iStaffPhoto.ImageUrl = Configuration.DefaultImageFullPath;

                userService = new UserService();
                PopulateEmployeeNameDropDownList(userService.GetEmployees(cbIncludeTerminatedStaff.Checked));

                Employee queriedEmployee;
                if (Request.QueryString.HasKeys())
                {
                    int employeeId = 0;
                    Int32.TryParse(Request.QueryString["EmployeeId"], out employeeId);

                    if (employeeId > 0)
                    {
                        queriedEmployee = userService.GetEmployee(employeeId);

                        if (queriedEmployee != null)
                        {
                            ddlStaffMembers.SelectedValue = queriedEmployee.employeeId.ToString();
                            ddlStaffMembers_SelectedIndexChanged(ddlStaffMembers, new EventArgs());
                        }

                    }
                }

                documentService = new DocumentService();
                PopulateDocumentTypeDropDownList(documentService.GetDocumentTypes());

                BuildRoleGrid();

                ToggleControlsOnEmployeeSelection();

                LockDownUnauthorizedUsers();
            }

            ResetFormValidation();
        }

        protected void ddlStaffMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            userService = new UserService();
            Employee selectedEmployee = userService.GetEmployee(Int32.Parse(ddlStaffMembers.SelectedValue));

            if (ddlStaffMembers.SelectedIndex > 0)
            {
                if (selectedEmployee != null)
                {
                    tbFirstName.Text = selectedEmployee.firstName;
                    tbMiddleInit.Text = selectedEmployee.middleInitial;
                    tbLastName.Text = selectedEmployee.lastName;
                    tbEmailAddress.Text = selectedEmployee.emailAddress;
                    tbPhoneNumber.Text = selectedEmployee.phoneNumber;
                    tbUserName.Text = selectedEmployee.userName;
                    cbCurrentEmployee.Checked = selectedEmployee.isEmployed;
                    tbUserName.Enabled = false;

                    iStaffPhoto.ImageUrl = GetEmployeeImagePath(selectedEmployee);

                    bUpdatePermissions.Visible = true;
                    UpdateRoleGridWithUserAccess(selectedEmployee);
                }
            }
            else
            {
                ClearEmployeeForm();
            }

            BindEmployeeNotes(selectedEmployee);
            BindEmployeeDocuments(selectedEmployee);
            ToggleControlsOnEmployeeSelection();
        }

        protected void bAddUpdateStaff_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            String statusMessage = ValidateAddUpdateEmployee(btn.CommandName);

            if (statusMessage.Length == 0)
            {
                userService = new UserService();

                Employee employee;

                if (ddlStaffMembers.SelectedIndex > 0)
                {
                    employee = userService.GetEmployee(Int32.Parse(ddlStaffMembers.SelectedValue));
                }
                else
                {
                    employee = new Employee(tbFirstName.Text, tbLastName.Text, userService.ParsePhoneNumberFromControl(tbPhoneNumber), userService.ParseEmailAddressFromControl(tbEmailAddress), cbCurrentEmployee.Checked, tbUserName.Text, Int32.Parse(ddlStaffMembers.SelectedValue), userService.ParseMiddleInitial(tbMiddleInit));
                }

                Password pwd = PasswordService.EncodePassword(tbPassword.Text);

                if (fuEmployeePhoto.HasFile)
                {
                    imageService = new ImageService();
                    String uniqueFileName = imageService.UploadImage(fuEmployeePhoto.FileName, employee.userName);

                    employee.employeeImage = imageService.AddImage(uniqueFileName);
                }

                switch (btn.CommandName)
                {
                    case "Add":
                        userService.AddEmployee(employee, pwd.hashedPassword, pwd.salt);
                        statusMessage = "<h3>Success!</h3>Employee added successfully.<br />";

                        break;
                    case "Update":
                        employee.firstName = tbFirstName.Text;
                        employee.middleInitial = userService.ParseMiddleInitial(tbMiddleInit);
                        employee.lastName = tbLastName.Text;

                        employee.phoneNumber = userService.ParsePhoneNumberFromControl(tbPhoneNumber);
                        employee.emailAddress = userService.ParseEmailAddressFromControl(tbEmailAddress);

                        employee.userName = tbUserName.Text;
                        employee.isEmployed = cbCurrentEmployee.Checked;

                        userService.UpdateEmployee(employee);
                        statusMessage = "<h3>Success!</h3>Employee updated successfully.<br />";
                        break;
                }

                PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("mpeStatus"), statusMessage);
                PopulateEmployeeNameDropDownList(userService.GetEmployees(cbIncludeTerminatedStaff.Checked));

                if (ddlStaffMembers.Items.FindByText(employee.fullname) != null)
                {
                    ddlStaffMembers.Items.FindByText(employee.fullname).Selected = true;
                }
                else
                {
                    ClearEmployeeForm();
                }

                iStaffPhoto.ImageUrl = GetEmployeeImagePath(employee);

                ToggleControlsOnEmployeeSelection();
            }
            else
            {
                PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("mpeStatus"), statusMessage);
            }
        }

        protected void cbIncludeTerminatedStaff_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            bool retainSelection = false;

            userService = new UserService();
            Employee selectedEmployee = userService.GetEmployee(Int32.Parse(ddlStaffMembers.SelectedValue));

            if (selectedEmployee != null)
            {
                if (selectedEmployee.isEmployed || cb.Checked)
                {
                    retainSelection = true;
                }
            }

            PopulateEmployeeNameDropDownList(userService.GetEmployees(cbIncludeTerminatedStaff.Checked));

            if (retainSelection)
            {
                ddlStaffMembers.SelectedValue = selectedEmployee.employeeId.ToString();
                ddlStaffMembers_SelectedIndexChanged(ddlStaffMembers, new EventArgs());
            }
            else
            {
                ClearEmployeeForm();
            }
        }

        protected void bUpdatePassword_Click(object sender, EventArgs e)
        {
            String statusMessage = ValidatePasswordReset();

            if (statusMessage.Length == 0)
            {
                String userName = tbUserName.Text;
                String newPassword = tbNewPassword.Text;

                userService = new UserService();
                Password pwd = PasswordService.EncodePassword(newPassword);

                if (userService.UpdatePassword(userName, pwd.hashedPassword))
                {
                    userService.UpdateSalt(userName, pwd.salt);
                    statusMessage = "<h3>Success!</h3>Password successfully updated.<br />";
                }
                else
                {
                    statusMessage = "<h3>An error occurred!</h3>Password could not be updated.<br />";
                }

                PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("mpeStatus"), statusMessage);
            }
            else
            {
                PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("mpeStatus"), statusMessage);
            }
        }

        protected void bUpdatePermissions_Click(object sender, EventArgs e)
        {
            String userName = tbUserName.Text;
            String[] currentRoles = Roles.GetRolesForUser(userName);

            foreach (GridViewRow gvr in gvRoles.Rows)
            {
                CheckBox cbHasAccess = (CheckBox)gvr.FindControl("cbHasAccess");
                String roleName = gvr.Cells[0].Text;

                if (cbHasAccess.Checked)
                {
                    if (!currentRoles.Contains(roleName))
                    {
                        Roles.AddUserToRole(userName, roleName);
                    }
                }
                else
                {
                    if (currentRoles.Contains(roleName))
                    {
                        Roles.RemoveUserFromRole(userName, roleName);
                    }
                }
            }

            PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("mpeStatus"), "<h3>Success!</h3>Roles updated successfully.<br />");
            LockDownUnauthorizedUsers();
        }



        protected void staffTab_Click(object sender, EventArgs e)
        {
            LinkButton clickedTab = (LinkButton)sender;

            switch (clickedTab.ID)
            {
                case "lbStaffInfoTab":
                    pnlStaffInfo.Visible = true;
                    pnlStaffDetails.Visible = false;
                    pnlStaffAccess.Visible = false;

                    lbStaffInfoTab.CssClass = "tab selected";
                    lbStaffDetailsTab.CssClass = "tab";
                    lbStaffAccessTab.CssClass = "tab";

                    break;
                case "lbStaffDetailsTab":
                    pnlStaffInfo.Visible = false;
                    pnlStaffDetails.Visible = true;
                    pnlStaffAccess.Visible = false;

                    lbStaffInfoTab.CssClass = "tab";
                    lbStaffDetailsTab.CssClass = "tab selected";
                    lbStaffAccessTab.CssClass = "tab";

                    break;
                case "lbStaffAccessTab":
                    pnlStaffInfo.Visible = false;
                    pnlStaffDetails.Visible = false;
                    pnlStaffAccess.Visible = true;

                    lbStaffInfoTab.CssClass = "tab";
                    lbStaffDetailsTab.CssClass = "tab";
                    lbStaffAccessTab.CssClass = "tab selected";

                    break;
            }
        }

        protected void bAddUpdateDocument_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            documentService = new DocumentService();
            userService = new UserService();
            Employee selectedEmployee = userService.GetEmployee(Int32.Parse(ddlStaffMembers.SelectedValue));

            String statusMessage = String.Empty;
            switch (button.CommandName)
            {
                case "Add":
                    break;
                case "Update":
                    break;
            }

            PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("mpeStatus"), statusMessage);
        }

        protected void ValidatePhoneNumber(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;
        }

        /* Helper Methods */

        protected void BuildRoleGrid()
        {
            membershipService = new MembershipService();

            gvRoles.DataSource = membershipService.GetAllRoles(Membership.ApplicationName);
            gvRoles.DataBind();
        }

        protected void ToggleControlsOnEmployeeSelection()
        {
            bool hasSelection = ddlStaffMembers.SelectedIndex > 0;

            switch (hasSelection)
            {
                case true:
                    bAddUpdateStaff.Text = "Update Staff Member";
                    bAddUpdateStaff.CommandName = "Update";

                    break;
                case false:
                    bAddUpdateStaff.Text = "Add Staff Member";
                    bAddUpdateStaff.CommandName = "Add";

                    break;
            }

            /* Enabled if theres a selection */
            bUpdatePassword.Enabled = hasSelection;
            tbConfirmNewPassword.Enabled = hasSelection;
            tbNewPassword.Enabled = hasSelection;

            bUpdatePermissions.Enabled = hasSelection;
            foreach (GridViewRow gvr in gvRoles.Rows)
            {
                CheckBox cbHasAccess = (CheckBox)gvr.FindControl("cbHasAccess");
                cbHasAccess.Enabled = hasSelection;
            }

            /* These are hidden / disabled if theres a selection, hence the ! */
            tbPassword.Enabled = !hasSelection;
            lblPassword.Visible = !hasSelection;
            tbPassword.Visible = !hasSelection;
        }

        protected void UpdateRoleGridWithUserAccess(Employee emp)
        {
            String[] usersRoles = Roles.GetRolesForUser(emp.userName);

            foreach (GridViewRow gvr in gvRoles.Rows)
            {
                CheckBox cbHasAccess = (CheckBox)gvr.FindControl("cbHasAccess");
                cbHasAccess.Checked = usersRoles.Contains(gvr.Cells[0].Text);
            }
        }

        protected void PopulateEmployeeNameDropDownList(List<Employee> employees)
        {
            ddlStaffMembers.AppendDataBoundItems = true;
            ddlStaffMembers.Items.Clear();
            ddlStaffMembers.Items.Add(new ListItem("Select One", "0"));
            ddlStaffMembers.DataTextField = "FullName";
            ddlStaffMembers.DataValueField = "EmployeeId";
            ddlStaffMembers.DataSource = employees;
            ddlStaffMembers.DataBind();
        }

        protected void PopulateDocumentTypeDropDownList(List<DocumentType> dTypes)
        {
            ddlDocumentTypes.AppendDataBoundItems = true;
            ddlDocumentTypes.Items.Clear();
            ddlDocumentTypes.Items.Add(new ListItem("Select One", "0"));
            ddlDocumentTypes.DataTextField = "DocumentTypeName";
            ddlDocumentTypes.DataValueField = "DocumentTypeId";
            ddlDocumentTypes.DataSource = dTypes;
            ddlDocumentTypes.DataBind();
        }

        protected void ClearEmployeeForm()
        {
            tbUserName.Enabled = true;

            tbFirstName.Text = String.Empty;
            tbMiddleInit.Text = String.Empty;
            tbLastName.Text = String.Empty;
            tbPhoneNumber.Text = String.Empty;
            tbEmailAddress.Text = String.Empty;
            cbCurrentEmployee.Checked = true;
            tbPassword.Text = String.Empty;
            tbUserName.Text = String.Empty;
            iStaffPhoto.ImageUrl = Configuration.DefaultImageFullPath;

            foreach (GridViewRow gvr in gvRoles.Rows)
            {
                CheckBox cbHasAccess = (CheckBox)gvr.FindControl("cbHasAccess");
                cbHasAccess.Enabled = false;
                cbHasAccess.Checked = false;
            }
        }


        protected void LockDownUnauthorizedUsers()
        {
            userService = new UserService();
            int employeeId = Int32.Parse(HttpContext.Current.Profile.GetPropertyValue("EmployeeId").ToString());
            Employee emp = userService.GetEmployee(employeeId);

            if (emp != null)
            {
                ddlStaffMembers.SelectedValue = employeeId.ToString();
                ddlStaffMembers_SelectedIndexChanged(ddlStaffMembers, new EventArgs());

                ToggleControlsOnEmployeeSelection();
            }

            if (!Roles.IsUserInRole("Personnel"))
            {
                ddlStaffMembers.Enabled = false;
                cbIncludeTerminatedStaff.Enabled = false;

                bUpdatePermissions.Enabled = false;

                foreach (GridViewRow gvr in gvRoles.Rows)
                {
                    CheckBox cbHasAccess = (CheckBox)gvr.FindControl("cbHasAccess");
                    cbHasAccess.Enabled = false;
                }
            }
        }

        protected String GetEmployeeImagePath(Employee emp)
        {
            if (emp.employeeImage.imageId != 1)
            {
                return Configuration.ImagePath + emp.employeeImage.fileName;
            }
            else
            {
                return Configuration.DefaultImageFullPath;
            }
        }

        protected String ValidateAddUpdateEmployee(String addUpdate)
        {
            String statusMessage = "<h3>Form Incomplete</h3><ul style=\"text-align: left\">";

            if (tbFirstName.Text.Length == 0)
            {
                tbFirstName.BackColor = ColorTranslator.FromHtml("#3D3D3D"); ;
                tbFirstName.ForeColor = Color.White;
                statusMessage += "<li>First name is required.</li>";
            }
            else
            {
                tbFirstName.BackColor = Color.White;
                tbFirstName.ForeColor = Color.Black;
            }

            if (tbLastName.Text.Length == 0)
            {
                tbLastName.BackColor = ColorTranslator.FromHtml("#3D3D3D"); ;
                tbLastName.ForeColor = Color.White;

                statusMessage += "<li>Last name is required.</li>";
            }
            else
            {
                tbLastName.BackColor = Color.White;
                tbLastName.ForeColor = Color.Black;
            }

            if (tbEmailAddress.Text.Length > 0)
            {
                if (!tbEmailAddress.Text.Contains('@') && !tbEmailAddress.Text.Contains('.'))
                {
                    statusMessage += "<li>Email address must be formatted like: example@ex.com. </li>";
                    tbEmailAddress.BackColor = ColorTranslator.FromHtml("#3D3D3D");
                    tbEmailAddress.ForeColor = Color.White;
                }
                else
                {
                    tbEmailAddress.BackColor = Color.WhiteSmoke;
                    tbEmailAddress.ForeColor = Color.Black;
                }
            }
            else
            {
                tbEmailAddress.BackColor = Color.WhiteSmoke;
                tbEmailAddress.ForeColor = Color.Black;
            }

            if (tbPhoneNumber.Text.Length > 0)
            {
                if (tbPhoneNumber.Text.Contains('_') && tbPhoneNumber.Text != "(___) ___-____")
                {
                    statusMessage += "<li>Phone number must be formatted like: (310) 555-4444. </li>";
                    tbPhoneNumber.BackColor = ColorTranslator.FromHtml("#3D3D3D");
                    tbPhoneNumber.ForeColor = Color.White;
                }
                else
                {
                    tbPhoneNumber.BackColor = Color.WhiteSmoke;
                    tbPhoneNumber.ForeColor = Color.Black;
                }
            }
            else
            {
                tbPhoneNumber.BackColor = Color.WhiteSmoke;
                tbPhoneNumber.ForeColor = Color.Black;
            }

            if (addUpdate == "Add" && tbUserName.Text.Length == 0)
            {
                tbUserName.BackColor = ColorTranslator.FromHtml("#3D3D3D");
                tbUserName.ForeColor = Color.White;

                statusMessage += "<li>User name required.</li>";
            }
            else
            {
                userService = new UserService();

                //Checking to make sure the username doesnt exist.
                Employee emp = userService.GetEmployee(tbUserName.Text);

                if (emp != null && addUpdate == "Add")
                {
                    tbUserName.BackColor = ColorTranslator.FromHtml("#3D3D3D");
                    tbUserName.ForeColor = Color.White;

                    statusMessage += "<li>Username already in use.</li>";
                }
                else
                {
                    tbUserName.BackColor = Color.White;
                    tbUserName.ForeColor = Color.Black;
                }
            }

            if (addUpdate == "Add" && tbPassword.Text.Length == 0)
            {
                tbPassword.BackColor = ColorTranslator.FromHtml("#3D3D3D");
                tbPassword.ForeColor = Color.White;

                statusMessage += "<li>Password required.</li>";
            }
            else
            {
                tbPassword.BackColor = Color.White;
                tbPassword.ForeColor = Color.Black;
            }

            if (fuEmployeePhoto.HasFile)
            {
                String fileName = fuEmployeePhoto.FileName;

                String[] fileNameParts = fileName.Split('.');

                if (fileNameParts.Length != 2)
                {
                    statusMessage += "<li>The file name was not formed correctly. </li>";
                }
                else
                {
                    String extension = fileNameParts[1];

                    if (extension.ToLower() != "jpg" && extension.ToLower() != "png" && extension.ToLower() != "gif")
                    {
                        statusMessage += "<li>Photos must be in png, jpg or gif file formats.</li>";
                    }

                }
            }

            if (statusMessage == "<h3>Form Incomplete</h3><ul style=\"text-align: left\">")
            {
                return String.Empty;
            }

            else
            {
                statusMessage += "</ul>";

                return statusMessage;
            }
        }

        protected String ValidatePasswordReset()
        {

            String statusMessage = "<h3>Form Incomplete</h3><ul style=\"text-align: left\">";


            if (tbConfirmNewPassword.Text != tbNewPassword.Text)
            {
                statusMessage += "<li>Passwords do not match.</li>";
            }

            if (tbNewPassword.Text.Length == 0)
            {
                tbNewPassword.BackColor = ColorTranslator.FromHtml("#3D3D3D"); ;
                tbNewPassword.ForeColor = Color.White;

                statusMessage += "<li>Enter a new password.</li>";
            }
            else
            {
                tbNewPassword.BackColor = Color.White;
                tbNewPassword.ForeColor = Color.Black;
            }

            if (tbConfirmNewPassword.Text.Length == 0)
            {
                tbConfirmNewPassword.BackColor = ColorTranslator.FromHtml("#3D3D3D"); ;
                tbConfirmNewPassword.ForeColor = Color.White;

                statusMessage += "<li>Enter your new password again to confirm.</li>";
            }
            else
            {
                tbConfirmNewPassword.BackColor = Color.White;
                tbConfirmNewPassword.ForeColor = Color.Black;
            }


            if (statusMessage == "<h3>Form Incomplete</h3><ul style=\"text-align: left\">")
            {
                return String.Empty;
            }

            else
            {
                statusMessage += "</ul>";

                return statusMessage;
            }
        }

        protected void DisplayAddUpdatePopup_Click(object sender, EventArgs e)
        {
            if (ddlStaffMembers.SelectedIndex == 0)
            {
                PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Master.FindControl("mpeStatus"), "<h3>An Error Occurred</h3>Please select a staff member.<br />" );
                return;
            }

            LinkButton clickedListBox = (LinkButton)sender;


            switch (clickedListBox.ClientID)
            {
                case "lbAddUpdateEmployeeNotes":
                    mpeStaffNotes.Show();

                    break;
                case "lbAddUpdateEmployeeDocuments":
                    mpeStaffDocuments.Show();

                    break;
            }
        }

        protected void ResetFormValidation()
        {
            tbFirstName.BackColor = Color.White;
            tbFirstName.ForeColor = Color.Black;
            tbLastName.BackColor = Color.White;
            tbLastName.ForeColor = Color.Black;
            tbEmailAddress.BackColor = Color.WhiteSmoke;
            tbEmailAddress.ForeColor = Color.Black;
            tbPhoneNumber.BackColor = Color.WhiteSmoke;
            tbPhoneNumber.ForeColor = Color.Black;
            tbUserName.BackColor = Color.White;
            tbUserName.ForeColor = Color.Black;
            tbPassword.BackColor = Color.White;
            tbPassword.ForeColor = Color.Black;
            tbNewPassword.BackColor = Color.White;
            tbNewPassword.ForeColor = Color.Black;
            tbConfirmNewPassword.BackColor = Color.White;
            tbConfirmNewPassword.ForeColor = Color.Black;
        }

        protected void btnEmployeeNotesPopupButton_Click(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            String statusMessage = String.Empty;

            if (clickedButton.ClientID == "btnStaffNotesPopupCancel")
            {
                tbStaffNote.Text = String.Empty;
                return;
            }
            else
            {
                if (ddlStaffMembers.SelectedIndex == 0)
                {
                    statusMessage = "<h3>Error</h3>Please choose a staff member.<br />";
                }
                else
                {
                    MembershipUser mu = Membership.GetUser();
                    UserService us = new UserService();

                    Employee loggedInEmployee = us.GetEmployee(mu.UserName);
                    Employee notedEmployee = us.GetEmployee(Int32.Parse(ddlStaffMembers.SelectedValue));

                    bool successful = false;

                    if (loggedInEmployee != null && notedEmployee != null)
                    {
                        Note note = new Note();

                        note.notedBy = loggedInEmployee;
                        note.noteOwner = notedEmployee;
                        note.noteText = tbStaffNote.Text;
                        note.noteType = "EmployeeNote";

                        successful = us.AddEmployeeNote(note);
                        tbStaffNote.Text = String.Empty;
                    }

                    if (successful)
                    {
                        statusMessage = "<h3>Success! </h3>The note was added successfully.<br />";

                        BindEmployeeNotes(notedEmployee);
                    }
                    else
                    {
                        statusMessage = "<h3>An Error Occurred</h3>The note could not be added at this time.<br />";
                    }
                }
            }

            tbStaffNote.Text = String.Empty;
            mpeStaffNotes.Hide();

            PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Master.FindControl("mpeStatus"), statusMessage);
        }

        protected void btnEmployeeDocumentsPopup_Click(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            String statusMessage = String.Empty;

            if (clickedButton.ClientID == "btnStaffDocumentsPopupCancel")
            {
            }
            else
            {
                if (ddlStaffMembers.SelectedIndex == 0)
                {
                    statusMessage = "<h3>Error</h3>Please choose a staff member.<br />";
                }
                else
                {
                    MembershipUser mu = Membership.GetUser();
                    UserService us = new UserService();

                    Employee selectedEmployee = us.GetEmployee(Int32.Parse(ddlStaffMembers.SelectedValue));

                    bool successful = false;

                    if (selectedEmployee != null)
                    {
                        
                    }

                    if (successful)
                    {
                        statusMessage = "<h3>Success! </h3>The note was added successfully.<br />";

                        BindEmployeeDocuments(selectedEmployee);
                   } 
                    else
                    {
                        statusMessage = "<h3>An Error Occurred</h3>The note could not be added at this time.<br />";
                    }
                }
            }

            PopupService.ShowPopup((AjaxControlToolkit.ModalPopupExtender)Master.FindControl("mpeStatus"), statusMessage);
            mpeStaffDocuments.Hide();

            tbDocumentName.Text = String.Empty;
            tbDocumentExpirationDate.Text = String.Empty;
            ddlDocumentTypes.SelectedIndex = 0;
        }

        private void BindEmployeeNotes(Employee emp)
        {
            if (emp != null)
            {
                userService = new UserService();
                rptEmployeeNotes.DataSource = userService.GetEmployeeNotes(emp);
            }
            else
            {
                rptEmployeeNotes.DataSource = new DataTable();
            }

            rptEmployeeNotes.DataBind();
        }

        private void BindEmployeeDocuments(Employee emp)
        {
            if (emp != null)
            {
                rptEmployeeDocuments.DataSource = new DataTable();
            }
            else
            {
                rptEmployeeDocuments.DataSource = new DataTable();
            }

            rptEmployeeDocuments.DataBind();
        }
    }
}